const app = getApp();
var Mcaptcha = require('../../utils/mcaptcha.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codename: '获取验证码',
    disabled: true,
    num: '',
    change: '切换帐号绑定',
    status: 'tel',
    Tel: '',
    inputYzm: '',
    inputPyzm: '',
    Uname: '',
    Upwd: '',
    ip: '124.239.176.230',
    sbtype: 0,
    sbname: 'android',
    LY: '小程序',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: 'https://zsbpt.zaisubao.cn/index.php/Api/Feijiu/getIp',
      data: {
      },
      method: 'get',
      success: function (r) {
        that.setData({
          ip: r.data.ip
        })
      }
    });
    wx.getSystemInfo({
      success: function (e) {
        if (e.platform == 'android') {
          that.setData({
            sbtype: 1
          })
        } else {
          that.setData({
            sbtype: 2
          })
        }
        that.setData({
          sbname: e.brand + e.model
        })
      }
    })


  },
  changeBind: function () {
    if (this.data.status == 'tel') {
      this.setData({
        change: '切换手机号绑定',
        status: 'user'
      })
    } else {
      this.setData({
        change: '切换帐号绑定',
        status: 'tel'
      })
      this.changePyzm();
    }
  },
  inputTel: function (e) {
    this.setData({
      Tel: e.detail.value
    })
  },
  inputPyzm: function (e) {
    this.setData({
      inputPyzm: e.detail.value
    })
  },
  inputYzm: function (e) {
    this.setData({
      inputYzm: e.detail.value
    })
  },
  inputPass: function (e) {
    this.setData({
      Upwd: e.detail.value
    })
  },
  inputUserName: function (e) {
    this.setData({
      Uname: e.detail.value
    })
  },
  logingetyzm: function () {
    this.getCode();
  },
  logingetyzmN: function () {
    // body...
  },
  getRanNum: function () {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var pwd = '';
    for (var i = 0; i < 4; i++) {
      if (Math.random() < 48) {
        pwd += chars.charAt(Math.random() * 48 - 1);
      }
    }
    return pwd;
  },

  /**sn.toUpperCase()
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.changePyzm();
  },

  changePyzm: function () {
    var that = this;
    var num = that.getRanNum();
    this.setData({
      num: num
    })
    new Mcaptcha({
      el: 'canvas',
      width: 80,//对图形的宽高进行控制
      height: 40,
      code: num
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCode: function () {
    var that = this;
    var tel = this.data.Tel;
    var myreg = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (!myreg.test(tel)) {
      app.tishi('请输入正确的手机号');
      return false;
    } else if (this.data.inputPyzm.toUpperCase() != this.data.num.toUpperCase()) {
      app.tishi('请输入正确的图形验证码');
      return false;
    } else {
      var api_input = {
        tel: this.data.Tel
      }
      app.getDaoPost('api/FeijiuApp/PostSendYzm_SMS_New', api_input, function (r) {
        if (r.code == 200) {
          app.tishi(r.data.Mess);
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: true,
              })
            } else {
              that.setData({
                codename: num + "s后重新获取",
                disabled: false,
              })
            }
          }, 1000)
        } else {
          app.tishi(r.message)
        }
      })
    }
  },
  loginBtn: function () {
    if (this.data.status == 'user') {
      if (this.data.Uname.length < 1) {
        app.tishi('请输入您的帐号/绑定帐号的手机号')
        return false
      }
      if (this.data.Upwd.length < 1) {
        app.tishi('请输入密码')
        return false
      }
      var that = this
      var api_input = {
        Uname: this.data.Uname,
        Upwd: this.data.Upwd,
        LY: this.data.LY
      }
      app.getDaoPost('api/FeijiuApp/Post_FJ_LoginLog', api_input, function (res) {
        if (res.code == 200) {
          wx.setStorageSync('FjLoginLog', res.data);
          that.PostLogin_WxBind(res.data.UID);
        } else {
          app.tishi(res.message)
        }
      })
    }
    if (this.data.status == 'tel') {
      var that = this
      var tel = this.data.Tel;
      var myreg = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
      if (!myreg.test(tel)) {
        app.tishi('请输入正确的手机号');
        return false;
      }
      var api_input = {
        Tel: this.data.Tel,
        YZM: this.data.inputYzm,
        zcly: this.data.zcly,
        LY: this.data.LY,
        IP: this.data.ip,
        sbid: '',
        sbtype: this.data.sbtype,
        sbname: this.data.sbname
      }
      app.getDaoPost('api/FeijiuApp/PostLoginLog_SMS', api_input, function (r) {
        if (r.code == 200) {
          app.tishi(r.message);
          wx.setStorageSync('FjLoginLog', r.data);
          // setTimeout(function () {
          //   wx.navigateBack();
          // }, 300)  
          that.PostLogin_WxBind(r.data.UID)
          console.log(r)
        } else {
          app.tishi(r.message)
        }
      })
    }
  },

  PostLogin_WxBind: function (uid) {
    var openid = wx.getStorageSync('openid');
    var userinfo = wx.getStorageSync('userinfo');
    var api_input = {
      UnionID: openid.unionid,
      OpenID: openid.openid,
      nickName: userinfo.userInfo.nickName,
      AesData: userinfo.encryptedData,
      AesKey: openid.session_key,
      AesIv: userinfo.iv,
      uid: uid,
      Ly: 1,
    }
    app.getDaoPost('api/FeijiuApp/PostLogin_WxBind', api_input, function (r) {
      if (r.code == 200) {
        app.tishi('绑定成功!')
        setTimeout(function () {
          wx.navigateBack({ delta: 2 });
        }, 2000)
      } else {
        app.tishi(r.message)
      }
    })
  },

})


